---
title: "如何测试Java代码"
date: 2019-10-9
categories:
- java
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgf760c5a">DataJpaTest 注解</a></li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgf760c5a">
<h2 id="orgf760c5a">DataJpaTest 注解</h2>
<div class="outline-text-2" id="text-orgf760c5a">
<p>
一般与 @RunWith(SpringRunner.class) 一起来测试JPA代码。
这个注解将禁用所有自动配置(AutoConfiguration)，只应用JPA相关的配置。
</p>
<p>
默认情况下，DataJpaTest注解的测试类将实例化一个内存数据库。使用 AutoConfigureTestDatabase 注解能重写这种行为。
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">使用内存数据库</span>
<span style="font-weight: bold; text-decoration: underline;">@DataJpaTest</span>(excludeAutoConfiguration = {FlywayAutoConfiguration.<span style="font-weight: bold;">class</span>, ApolloAutoConfiguration.<span style="font-weight: bold;">class</span>})
<span style="font-weight: bold; text-decoration: underline;">@RunWith</span>(SpringRunner.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold; text-decoration: underline;">@EnableJpaAuditing</span>
<span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold; font-style: italic;">SomethingTest</span> {
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">这里面能够注入JPA相关的Bean，例如Repository</span>
    <span style="font-weight: bold; text-decoration: underline;">@Autowired</span> <span style="font-weight: bold; text-decoration: underline;">SomethingRepository</span> <span style="font-weight: bold; font-style: italic;">repository</span>;
}

<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">使用外部数据库</span>
<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">AutoConfigureTestDatabase 表示配置一个测试数据源(DataSource)，而不是自动配置的(AutoConfiguration)</span>
<span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">然后 ActiveProfiles 指定加载的配置文件，这将加载 application-local.yml 中配置的数据库</span>
<span style="font-weight: bold; text-decoration: underline;">@DataJpaTest</span>
<span style="font-weight: bold; text-decoration: underline;">@RunWith</span>(SpringRunner.<span style="font-weight: bold;">class</span>)
<span style="font-weight: bold; text-decoration: underline;">@ActiveProfiles</span>(<span style="font-style: italic;">"local"</span>)
<span style="font-weight: bold; text-decoration: underline;">@AutoConfigureTestDatabase</span>(replace = <span style="font-weight: bold; text-decoration: underline;">AutoConfigureTestDatabase</span>.<span style="font-weight: bold; text-decoration: underline;">Replace</span>.NONE)
<span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">void</span> <span style="font-weight: bold; font-style: italic;">SomethingTest</span> {

}
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-10-9</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
